package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.MsgTmpEditReqVo;
import com.miplus.ccrm.core.controller.vo.conf.MsgTmpReqVo;
import com.miplus.ccrm.core.controller.vo.conf.MsgTmpResVo;
import com.miplus.ccrm.core.pojo.MsgDefine;
import com.miplus.ccrm.core.mapper.MsgDefineMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author pc
* @description 针对表【msg_define】的数据库操作Service实现
* @createDate 2024-03-08 10:00:20
*/
@Service
public class MsgDefineServiceImpl extends MPJBaseServiceImpl<MsgDefineMapper, MsgDefine>
    implements MsgDefineService{

    @Override
    public IPage<MsgTmpResVo> msgTempList(MsgTmpReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                MsgTmpResVo.class,
                new MPJLambdaWrapper<MsgDefine>()
                        .selectAs("fun_getnames('PUBLIC.MORAUTO',t.sendmode)",MsgTmpResVo::getSendmodename)
                        .selectAll(MsgDefine.class)
                        .like(JPubUtils.IsNotEmpty(paras.getName()),"name",paras.getName())
                        .eq(JPubUtils.IsNotEmpty(paras.getStatus()),"status",paras.getStatus())
        );
    }

    @Override
    public CmdResVo updateMsgTmp(MsgTmpEditReqVo paras)
    {
        MsgDefine def=new MsgDefine();
        BeanUtils.copyProperties(paras,def);
        int ret= baseMapper.updateById(def);
        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo insertMsgTmp(MsgTmpEditReqVo paras)
    {
        MsgDefine def=new MsgDefine();
        BeanUtils.copyProperties(paras,def);
        int ret= baseMapper.insert(def);
        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo deleteMsgTmp(String id)
    {
        int ret= baseMapper.deleteById(id);
        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo updateStatus(String id,String status)
    {
        MsgDefine def=new MsgDefine();
        def.setId(id);
        def.setStatus(status);
        int ret=baseMapper.updateById(def);
        return new CmdResVo(ret);
    }

    @Override
    public List<LabelResVo> getMsgTmpList()
    {
        return selectJoinList(
                LabelResVo.class,
                new MPJLambdaWrapper<MsgDefine>()
                        .selectAs("msgid",LabelResVo::getValue)
                        .selectAs("name",LabelResVo::getLabel)
                        .select("msgid","name")
                        .eq("status","1")
        );
    }

    @Override
    public MsgTmpResVo getMsgTmpInfo(String tempID)
    {
        return selectJoinOne(
                MsgTmpResVo.class,
                new MPJLambdaWrapper<MsgDefine>()
                        .selectAs("(case when t.sendmode='1' then 'Y' else 'N' end)",MsgTmpResVo::getSendmodename)
                        .selectAll(MsgDefine.class)
                        .eq(MsgDefine::getId,tempID)
        );
    }
}




